 package com.es.hr.baoxiao;
 
 import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
 
 @SuppressWarnings("serial")
public class BaoxiaoDownload extends HttpServlet{
	 

	 public void init(ServletConfig servletConfig) throws ServletException {
		    super.init(servletConfig);

		    if(System.getProperty("appDir")==null){
			    String appDir = servletConfig.getServletContext().getRealPath("/");
			    System.getProperties().put("appDir", appDir);
		    }
	 }		    

	 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
     SmartUpload mySmartUpload = new SmartUpload();
     mySmartUpload.initialize(getServletConfig(), request, response);
    
     try{
    	 mySmartUpload.upload();
    	 Request req = mySmartUpload.getRequest();
    	 
    	 String fileName = req.getParameter("fileName");
    	  fileName = System.getProperty("appDir")+"upload\\baoxiao\\" + fileName;
    	    	  FileInputStream inStream = new FileInputStream(fileName);
          response.reset();    //必须reset，否则会出现文件不完整
          
          // 设定contentDisposition为null以禁止浏览器自动打开文件，
          //保证点击链接后是下载文件。若不设定，则下载的文件扩展名为
          //doc时，浏览器将自动用word打开它。扩展名为pdf时，
          //浏览器将用acrobat打开。
          mySmartUpload.setContentDisposition(null);            
          mySmartUpload.downloadFile(fileName);
        // 下载文件            
          
          byte[] b = new byte[100];
		int len;
		//循环取出流中的数据 
          while ((len = inStream.read(b)) > 0) {
              response.getOutputStream().write(b, 0, len);
          }
          inStream.close();

     }catch (Exception e) {
    	 e.printStackTrace();
    	 request.setAttribute("message", "文件不存在！");
    	 RequestDispatcher dis = request.getRequestDispatcher("/jsp/hr/baoxiao/error.jsp");
        // RequestDispatcher dis = request.getRequestDispatcher(imp_name);
         dis.forward(request, response);
         return;
     }
     request.setAttribute("message", "下载成功！");
	// RequestDispatcher dis = request.getRequestDispatcher("/jsp/hr/baoxiao/error.jsp");
    // RequestDispatcher dis = request.getRequestDispatcher(imp_name);
   //  dis.forward(request, response);
     return;
   }
 
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException{
     doPost(request, response);
   }
   

  
 }